<?
if(!POST) {
} elseif(!FILE) {
    switch ($params[2]){
        // MEDIA ALT
        case 'alt':
            if($_POST['ID'] && $page->page->media->$_POST['ID']) {
                $page->page->media->$_POST['ID']->alt = $_POST['value'];
                uygulama::set('data/'.$params[1].'/content', $page->page);
                $page = uygulama::result('200',$_POST);
            } else $page = uygulama::result('600',uygulama::translate('Media item not found!'));
        break;
        // MEDIA REMOVE
        case 'removeMedia':
            @unlink('data/'.$params[1].'/m'.$_POST['ID'].'.jpg');
            @unlink('data/'.$params[1].'/t'.$_POST['ID'].'.jpg');
            if($_POST['ID'] && $page->page->media->$_POST['ID']) {
                unset($page->page->media->$_POST['ID']);
                uygulama::set('data/'.$params[1].'/content', $page->page);
                $page = uygulama::result('200');
            } else $page = uygulama::result('600',uygulama::translate('Media item not found!'));
        break;
        // CONTENT SAVE
        default:
            $lang = LANG;
            $page->page->$lang->title = $_POST['title'] ? $_POST['title'] : $page->page->$lang->title;
            $page->page->$lang->description = $_POST['description'] ? $_POST['description'] : $page->page->$lang->description;
            $page->page->$lang->keywords = $_POST['keywords'] ? $_POST['keywords'] : $page->page->$lang->keywords;
            $page->page->$lang->content = $_POST['content'] ? $_POST['content'] : $page->page->$lang->content;
            uygulama::set('data/'.$params[1].'/content', $page->page);
            $page = uygulama::result('200',sprintf(_('"%s" subpage is saved!'),$params[1]));
        break;
    }
} else {	
    $ext = '.'.end(explode(".",strtolower($_FILES['Filedata']['name'])));
    // MEDIA ID
    $ID = (@count($page->page->media) +1).(microtime(1)*10000);
    // TMP FILE
    $page->page->media->$ID->tmp = 'data/'.$params[1].'/m'.$ID.$ext;
    // MEDIA UPLOAD
    uygulama::upload($_FILES['Filedata']['tmp_name'], $page->page->media->$ID->tmp);
    
    switch ($ext){
    	case '.jpeg':
    	case '.jpg':
    	case '.gif':
    	case '.png':
    		    	
    		//if(!$page->page->media) $page->page->media = new stdClass();	
    		$page->page->media->$ID->type = 'image';
		    $page->page->media->$ID->thumb = 'data/'.$params[1].'/t'.$ID.'.jpg';
		    $page->page->media->$ID->alt = $_FILES['Filedata']['name'];
		    $page->page->media->$ID->src = 'data/'.$params[1].'/m'.$ID.'.jpg';
		    
		    uygulama::imageresize($page->page->media->$ID->tmp, $page->page->media->$ID->src, 905);
		    uygulama::imageresize($page->page->media->$ID->src, $page->page->media->$ID->thumb, 160);
		    
    		break;
    	case '.pdf':
    	case '.doc':
    	case '.xls':
    	case '.ppt':
    	case '.odt':
    	case '.ods':
    	case '.odp':
    	case '.psd':
    	case '.fla':
    	case '.zip':
    	case '.gz':
    		    	
    		$page->page->media->$ID->type = $ext;
		    //$page->page->media->$ID->thumb = 'data/'.$params[1].'/t'.$ID.'.jpg';
		    //$page->page->media->$ID->thumb = 'data/'.$params[1].'/t'.$ID.'.jpg';
		    $page->page->media->$ID->alt = $_FILES['Filedata']['name'];
		    $page->page->media->$ID->src = 'data/'.$params[1].'/m'.$ID.$ext;
		    //copy($page->page->media->$ID->tmp, $page->page->media->$ID->src);
		    
    		break;
    	/*case 'mp3':
    	case 'acc':
    	case 'ogg':
    	case 'js':*/
    		
    	case '.mp4':
    	case '.f4v':
    	case '.flv':
    		    	
    		$page->page->media->$ID->type = 'video';
		    //$page->page->media->$ID->thumb = 'data/'.$params[1].'/t'.$ID.'.jpg';
		    $page->page->media->$ID->alt = $_FILES['Filedata']['name'];
		    $page->page->media->$ID->src = 'data/'.$params[1].'/m'.$ID.$ext;
		    //copy($page->page->media->$ID->tmp, $page->page->media->$ID->src);
		    
    		break;
    	case '.swf':
    		    	
    		$page->page->media->$ID->type = 'flash';
		    //$page->page->media->$ID->thumb = 'data/'.$params[1].'/t'.$ID.'.jpg';
		    $page->page->media->$ID->alt = $_FILES['Filedata']['name'];
		    $page->page->media->$ID->src = 'data/'.$params[1].'/m'.$ID.$ext;
		    //copy($page->page->media->$ID->tmp, $page->page->media->$ID->src);
		    
    		break;
    	
    	
    }
    
    // Delete tmp file
    if($page->page->media->$ID->tmp != $page->page->media->$ID->src)
    	@unlink($page->page->media->$ID->tmp);
    // Clear tmp
    unset($page->page->media->$ID->tmp);
    // SAVE CHANGES
    uygulama::set('data/'.$params[1].'/content', $page->page);
    // Set ID
    $page->page->media->$ID->ID = $ID;
    // Uploaded media data
    echo json_encode($page->page->media->$ID); exit;
}
?>